package com.wang.lottery.infrastucture.repository;

import cn.bugstack.middleware.db.router.annotation.DBRouter;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.lottery.domain.activity.model.po.UserStrategyExport;
import com.wang.lottery.domain.activity.model.vo.InvoiceVO;
import com.wang.lottery.domain.activity.repository.IUserStrategyExportRepository;
import com.wang.lottery.domain.award.model.vo.DrawOrderVO;
import com.wang.lottery.infrastucture.mapper.UserStrategyExportMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjh
 * @create 2023-11-09 16:32
 * 用户策略计算结果表仓储服务
 */
@Service
public class UserStrategyExportRepository extends ServiceImpl<UserStrategyExportMapper, UserStrategyExport> implements IUserStrategyExportRepository {

    /**
     * 保存抽奖信息
     *
     * @param drawOrder 中奖单
     */
    @Override
    @DBRouter(key = "uId")
    public void saveUserStrategyExport(DrawOrderVO drawOrder) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setuId(drawOrder.getuId());
        userStrategyExport.setActivityId(drawOrder.getActivityId());
        userStrategyExport.setOrderId(drawOrder.getOrderId());
        userStrategyExport.setStrategyId(drawOrder.getStrategyId());
        userStrategyExport.setStrategyMode(drawOrder.getStrategyMode());
        userStrategyExport.setGrantType(drawOrder.getGrantType());
        userStrategyExport.setGrantDate(drawOrder.getGrantDate());
        userStrategyExport.setGrantState(drawOrder.getGrantState());
        userStrategyExport.setAwardId(drawOrder.getAwardId());
        userStrategyExport.setAwardType(drawOrder.getAwardType());
        userStrategyExport.setAwardName(drawOrder.getAwardName());
        userStrategyExport.setAwardContent(drawOrder.getAwardContent());
        userStrategyExport.setUuid(String.valueOf(drawOrder.getOrderId()));
        userStrategyExport.setCreateTime(new Date());
        userStrategyExport.setUpdateTime(userStrategyExport.getCreateTime());
        this.save(userStrategyExport);
    }

    /**
     * 更新发货单MQ状态
     *
     * @param uId     用户id
     * @param orderId 订单id
     * @param code    发送状态
     */
    @Override
    public void updateInvoiceMqState(String uId, Long orderId, Integer code) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setuId(uId);
        userStrategyExport.setOrderId(orderId);
        userStrategyExport.setMqState(code);
        baseMapper.updateInvoiceMqState(userStrategyExport);
    }

    /**
     * 扫描发货单 MQ 状态，把未发送 MQ 的单子扫描出来，做补偿
     * @return 未发送成功的mq单子
     */
    @Override
    public List<InvoiceVO> scanInvoiceMqState() {
        List<UserStrategyExport> userStrategyExports = baseMapper.scanInvoiceMqState();

        return userStrategyExports.stream().map(userStrategyExport -> {
            InvoiceVO invoiceVO = new InvoiceVO();
            invoiceVO.setuId(userStrategyExport.getuId());
            invoiceVO.setOrderId(userStrategyExport.getOrderId());
            invoiceVO.setAwardType(userStrategyExport.getAwardType());
            invoiceVO.setAwardId(userStrategyExport.getAwardId());
            invoiceVO.setAwardName(userStrategyExport.getAwardName());
            invoiceVO.setAwardContent(userStrategyExport.getAwardContent());
            return invoiceVO;
        }).collect(Collectors.toList());
    }
}
